﻿using Autofac.Annotation;
using Microsoft.AspNetCore.Mvc;
using TS.Model;
using TS.Services;

namespace TaskScheduling.Controllers;

[Component]
[Route("/[controller]/[action]")]
public class TaskController : Controller
{
    [Autowired] public TaskService taskService { get; set; }

    [HttpGet]
    public async Task<IActionResult> List(TaskSearchModel search)
    {
        return Json(await taskService.GeList(search));
    }

    [HttpPost]
    public async Task<IActionResult> AddOrEdit([FromBody] TaskOptions task)
    {
        return Json(await taskService.AddOrUpdateTask(task));
    }

    [HttpPost]
    public async Task<IActionResult> Remove([FromBody] TaskOptions task)
    {
        return Json(await taskService.Delete(task.ids));
    }

    [HttpPost]
    public async Task<IActionResult> Pause([FromBody] TaskOptions task)
    {
        return Json(await taskService.pauseTask(task.ids));
    }

    [HttpPost]
    public async Task<IActionResult> Start([FromBody] TaskOptions task)
    {
        return Json(await taskService.startTask(task.ids));
    }

    public async Task<IActionResult> Run([FromBody] TaskOptions taskOptions)
    {
        return Json(await taskService.executeTask(taskOptions.ID));
    }

    [HttpGet]
    public async Task<IActionResult> Logs(string taskname, string groupname, int pageIndex, int pageSize)
    {
        return Json(await taskService.getHis(taskname, groupname, pageIndex, pageSize));
    }
}